package com.example.sunmiv1.activity;

import android.content.Context;
import android.os.Bundle;

import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.sunmiv1.R;
import com.example.sunmiv1.fragment.IndexFragment;
import com.example.sunmiv1.fragment.MyFragment;
import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.utils.CrashHandler;
//import com.tencent.bugly.crashreport.CrashReport;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends FragmentActivity {

    @BindView(R.id.main_frame)
    FrameLayout mainFrame;
    @BindView(R.id.main_index_iv)
    ImageView mainIndexIv;
    @BindView(R.id.main_index_tv)
    TextView mainIndexTv;
    @BindView(R.id.main_index_linear)
    LinearLayout mainIndexLinear;
    @BindView(R.id.main_my_iv)
    ImageView mainMyIv;
    @BindView(R.id.main_my_tv)
    TextView mainMyTv;
    @BindView(R.id.main_my_linear)
    LinearLayout mainMyLinear;

    private Context context;
    private IndexFragment indexFragment;
    private MyFragment myFragment;
    FragmentManager manager;
    private long firstTime = 0;
    private ACache aCache;
    public static MainActivity mainActivity;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        Locale.setDefault(Locale.TRADITIONAL_CHINESE);

        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        mainActivity = this;
        context = this;
        aCache = ACache.get(context);

        //CrashReport.putUserData(context, "url", aCache.getAsString("url"));
        //CrashReport.putUserData(context, "phone", aCache.getAsString("login_acnum"));
       // CrashReport.putUserData(context, "pass", aCache.getAsString("login_pass"));

        manager = getSupportFragmentManager();
        ButterKnife.bind(this);
        chooseItem(1);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mainActivity = null;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (aCache.getAsString("log") != null) {
            CrashHandler.sendCrashLog2PM(context, aCache.getAsString("log"));
        }
    }



    @OnClick({R.id.main_index_linear, R.id.main_my_linear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.main_index_linear:
                mainIndexIv.setImageResource(R.mipmap.main_index_x);
                mainIndexTv.setTextColor(context.getResources().getColor(R.color.login_textColor1));
                mainMyIv.setImageResource(R.mipmap.main_my_wx);
                mainMyTv.setTextColor(context.getResources().getColor(R.color.login_textColor2));
                chooseItem(1);
                break;
            case R.id.main_my_linear:
                mainMyIv.setImageResource(R.mipmap.main_my_x);
                mainMyTv.setTextColor(context.getResources().getColor(R.color.login_textColor1));
                mainIndexIv.setImageResource(R.mipmap.main_index_wx);
                mainIndexTv.setTextColor(context.getResources().getColor(R.color.login_textColor2));
                chooseItem(2);
                break;
        }
    }

    public void chooseItem(int index) {
        FragmentTransaction transaction = manager.beginTransaction();
        hideFragments(transaction);
        switch (index) {
            case 1:
                if (indexFragment == null) {
                    indexFragment = new IndexFragment();
                    transaction.add(R.id.main_frame, indexFragment);
                } else {
                    transaction.show(indexFragment);
                }
                break;
            case 2:
                if (myFragment == null) {
                    myFragment = new MyFragment();
                    transaction.add(R.id.main_frame, myFragment);
                } else {
                    transaction.show(myFragment);
                }
                break;
        }
        transaction.commit();
    }

    //隱藏所有的Fragment,避免fragment混亂

    private void hideFragments(FragmentTransaction transaction) {
        if (indexFragment != null) {
            transaction.hide(indexFragment);
        }
        if (myFragment != null) {
            transaction.hide(myFragment);
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                long secondTime = System.currentTimeMillis();
                if (secondTime - firstTime > 2000) {
                    Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                    firstTime = secondTime;
                    return true;
                } else {
                    System.exit(0);
                }
                break;
        }
        return super.onKeyUp(keyCode, event);
    }
}
